package com.hr.runnerbuddy.parcour;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class Settings extends Activity {
	
	public static final String PREFS_NAME = "savesettings";
	
	private EditText eftime;
	private EditText efdistance;
	//private boolean savetodb;
	
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        
        eftime = (EditText)findViewById(R.id.editText1);
    	efdistance = (EditText)findViewById(R.id.editText2);
    	
    	final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    	int mintime_save = settings.getInt("mintime", 10);
        int mindistance_save = settings.getInt("mindistance", 20);
        boolean todb_save = settings.getBoolean("savetodb", false);
        
        final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
        checkBox.setChecked(todb_save);
    	
    	eftime.setText(Integer.toString(mintime_save));
    	efdistance.setText(Integer.toString(mindistance_save));
    	
        final Button button = (Button) findViewById(R.id.opslaan);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            	if(eftime.getText().length() == 0){
            		eftime.setText("0");
            	}
            	
            	if(efdistance.getText().length() == 0){
            		efdistance.setText("0");
            	}
            	
	            int neweftime = Integer.parseInt(eftime.getText().toString());
	            	int newefdistance = Integer.parseInt(efdistance.getText().toString());
	            	SharedPreferences.Editor editor = settings.edit();
	            	
	            	if(!eftime.equals(neweftime) && !efdistance.equals(newefdistance)){
		                editor.putInt("mintime", neweftime);
		                editor.putInt("mindistance", newefdistance);
	            	}
	            	
	            	editor.putBoolean("savetodb", checkBox.isChecked());
	            	
	            	// Commit the edits!
	                editor.commit(); 	

            }
        });
    }

	@SuppressWarnings("rawtypes")
	public void navigateTo(Class c){
    	Intent intent = new Intent(Settings.this, c);
    	startActivity(intent);
    }
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.main:
        	navigateTo(Parcour.class);
            return true;
        case R.id.gpslist:
        	navigateTo(GpsList.class);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}
